'use strict';

const webpack = require('webpack');
const WebpackDevServer = require('webpack-dev-server');
const open = require('open');

const env = require('../../conf.js');
const config = require('./client.dev.js');
const compiler = webpack(config);

let bundleStart;

// on "Compile"
compiler.plugin('compile', () => {
    console.log('Start Bundling');
    bundleStart = Date.now();
});

// on "Compile Done"
compiler.plugin('done', () => {
    console.log(`Bundled in ${Date.now() - bundleStart} ms!`);
});

const bundler = new WebpackDevServer(compiler, {
    publicPath: '/static/',
    hot: true,
    quiet: false,
    noInfo: true,
    stats: {
        colors: true
    },
    proxy: {
        '**': `http://localhost:${env.API_MAIN_PORT}`
    }
});

bundler.listen(env.DEV_SERVER_PORT, () => {
    const path = `http://localhost:${env.DEV_SERVER_PORT}`;
    console.log('Webpack Dev Server running in:');
    console.log(path);
    open(path);
});